from flask import render_template, make_response, request
from flask_restful import Resource, fields, marshal_with, marshal, reqparse


from app.models import Type, Poem, poem_type, Dynasty, Author, Remark

info_classes = {
            't_id': fields.Integer(attribute='t_id'),
            't_name': fields.String(attribute='t_name'),
        }
info_authors = {
            'a_id': fields.Integer(attribute='a_id'),
            'a_name': fields.String(attribute='a_name'),
        }
info_dynastys = {
            'd_id': fields.Integer(attribute='d_id'),
            'd_name': fields.String(attribute='d_name'),
        }
# 分类
class ClassifyResource(Resource):
    def get(self):
        c_id = request.args.get('c_id')
        if c_id=="t":
            classes = Type.query.all()
            cities_info = {
                'returnCode': fields.Integer,
                'returnvalue': fields.Nested(info_classes)
            }
            return marshal({'returnCode': 200, 'returnvalue': classes}, cities_info)
        elif c_id=="a":
            authors = Author.query.all()
            cities_info = {
                'returnCode': fields.Integer,
                'returnvalue': fields.Nested(info_authors)
            }
            return marshal({'returnCode': 200, 'returnvalue': authors}, cities_info)
        elif c_id=="d":
            dynastys = Dynasty.query.all()
            cities_info = {
                'returnCode': fields.Integer,
                'returnvalue': fields.Nested(info_dynastys)
            }
            return marshal({'returnCode': 200, 'returnvalue': dynastys}, cities_info)



# 列表
cou_info = {
    "p_id": fields.Integer(attribute="p_id"),
    "p_name": fields.String(attribute="p_name"),
}
#定义返回json字段格式
cou_fields = {
    "status": fields.String,
    "course": fields.Nested(cou_info)
}
class Article_listsResource(Resource):
    @marshal_with(cou_fields)
    def get(self):
        c_id = request.args.get('c_id')
        if c_id=="t":
            id=request.args.get('id')
            types=Type.query.get(id)
            cous=types.poem.all()
            return {'status':'200','course':cous}
        elif c_id=="a":
            id=request.args.get('id')
            cous=Poem.query.filter(Poem.p_author_id == id).all()
            return {'status':'200','course':cous}
        elif c_id=="d":
            id=request.args.get('id')
            cous = Poem.query.filter(Poem.p_dynasty_id == id).all()
            return {'status':'200','course':cous}
        else:
            return {'status':'404'}


poem_all = {
    "p_id": fields.Integer(attribute="p_id"),
    "p_name": fields.String(attribute="p_name"),
    "p_content": fields.String(attribute="p_content"),
    "p_analyze": fields.String(attribute="p_analyze"),
    "p_annotation": fields.String(attribute="p_annotation"),
    "p_view": fields.String(attribute="p_view"),
    "p_collect_count": fields.String(attribute="p_collect_count"),
}
author_all = {
    "a_id": fields.Integer(attribute="a_id"),
    "a_name": fields.String(attribute="a_name"),
}

dynasty_all = {
    "d_id": fields.Integer(attribute="d_id"),
    "d_name": fields.String(attribute="d_name"),
}
remarks_all = {
    "r_id": fields.Integer(attribute="r_id"),
    "r_content": fields.String(attribute="r_content"),
}
poem_info = {
    'status': fields.Integer,
    'poems': fields.Nested(poem_all),
    'authors': fields.Nested(author_all),
    'dynastys': fields.Nested(dynasty_all),
    'remarks': fields.Nested(remarks_all),
}
# 详情页
class Article_detailsResource(Resource):
    @marshal_with(poem_info)
    def get(self):
        id = request.args.get('id')
        poems = Poem.query.filter(Poem.p_id == id).all()
        authors = [Author.query.filter(Author.a_id == i.p_author_id).first() for i in poems]
        dynastys = [Dynasty.query.filter(Dynasty.d_id == i.p_dynasty_id).first() for i in poems]
        remarks = [Remark.query.filter(Remark.r_id == i.p_remark_id).first() for i in poems]

        return {'status': 200,
                'poems': poems,
                'authors': authors,
                'dynastys': dynastys,
                'remarks': remarks,
                }